package com.example.cpoopc.helloretrofit.bean;

import android.os.Parcel;
import android.os.Parcelable;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

/**
 * 帖子列表实体
 * 
 * <br>Created 2014-8-22 上午9:40:01
 * @version  
 * @author   chenpeng		
 *
 * @see
 */
public class PostListEntity  implements Parcelable{
    public long max_ts;//（下次请求获取更新时使用。）
    public long min_ts;//（下次请求获取更多时使用。返回0，表示没有更多数据了）
    public long count;// 当前返回的记录数
    
    public ArrayList<PostEntity> topics;

    public PostListEntity(long max_ts, long min_ts, long count, ArrayList<PostEntity> topics) {
        super();
        this.max_ts = max_ts;
        this.min_ts = min_ts;
        this.count = count;
        this.topics = topics;
    }
    
    public int size(){
//        return topics==null?0:topics.size();
        return topics.size();
    }
    
    public PostEntity get(int position){
//        return topics==null?null:topics.get(position);
        return topics.get(position);
    }
    
    public void remove(int position){
        if(topics!=null&&position<topics.size())
        topics.remove(position);
    }

    @Override
    public String toString() {
        return "PostListEntity [max_ts=" + max_ts + ", min_ts=" + min_ts + ", count=" + count
                + ", topics=" + topics + "]";
    }

    @Override
    public int describeContents() {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        // TODO Auto-generated method stub
        
    }
    
//    public String toJsonString() throws JSONException{
//        return toJsonString(false);
//    }
//    public String toJsonString(boolean sendfail) throws JSONException{
//        JSONObject jb = new JSONObject();
//        jb.put("max_ts", max_ts);
//        jb.put("min_ts", min_ts);
//        jb.put("count", count);
//        if(topics!=null){
//            JSONArray ja = new JSONArray();
//            int size = topics.size();
////            if(size>200){
////                size = 200;
////            }
//            for (int i = 0; i < size; i++) {
//            	// 发送失败的另外保存
//            	if(sendfail){
////            	    if(sendfail||!topics.get(i).isSendFail){
//            		ja.put(topics.get(i).toJsonObject());
//            	}else if(!topics.get(i).isSendFail&&!topics.get(i).isSending){
//            	    // 保存服务端返回的
//            	    ja.put(topics.get(i).toJsonObject());
//            	}
//            }
//            jb.put("topics", ja);
//        }
//        return jb.toString();
//    }
    
    // 测试用
    public String getString() {
        JSONObject jb = new JSONObject();
        try {
            jb.put("max_ts", max_ts);
            jb.put("min_ts", min_ts);
            jb.put("count", count);
            if(topics!=null){
                JSONArray ja = new JSONArray();
                int size = topics.size();
//            if(size>200){
//                size = 200;
//            }
                for (int i = 0; i < size; i++) {
                    ja.put(topics.get(i).toJsonObject());
                }
                jb.put("topics", ja);
            }
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return jb.toString();
    }

}
